using UnityEngine;

namespace CHot
{
    public class BoxCollider : Collider
    {
        public Vector2 size;

        [HideInInspector]
        public Transform[] points = new Transform[4];

        //以下public标签不能去掉，作用是存储OnDrawGizmos()方法里调好的数值
        [HideInInspector]
        public Vector3[] poses = new Vector3[4];

        private void Awake()
        {
            for (int i = 0; i < 4; i++)
            {
                string name = "point_" + i;
                Transform point = new GameObject(name).transform;
                point.SetParent(transform, false);
                point.localPosition = poses[i];
                points[i] = point;
            }
        }

        public Vector2[] Get4Poses()
        {
            Vector2[] _poses = new Vector2[4];
            for (int i = 0; i < 4; i++)
            {
                _poses[i] = points[i].position;
            }
            return _poses;
        }

        private void FixedUpdate()
        {
            transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
        }


#if UNITY_EDITOR
        void OnDrawGizmos()
        {
            Gizmos.color = Color.yellow;
            transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);

            if (Application.isPlaying)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (i < 3)
                    {
                        Gizmos.DrawLine(points[i].position, points[i + 1].position);
                    }
                    else
                    {
                        Gizmos.DrawLine(points[i].position, points[0].position);
                    }
                }
            }
            else
            {
                float halfX = size.x * 0.5f;
                float halfY = size.y * 0.5f;
                float halfShowX = halfX * Mathf.Abs(transform.lossyScale.x);
                float halfShowY = halfY * Mathf.Abs(transform.lossyScale.y);
                Vector3[] showPos = new Vector3[4];
                for (int i = 0; i < 4; i++)
                {
                    if (i == 0)
                    {
                        poses[i] = new Vector3(-halfX, halfY, 0);
                        showPos[i] = new Vector3(-halfShowX, halfShowY, 0);
                    }
                    else if (i == 1)
                    {
                        poses[i] = new Vector3(-halfX, -halfY, 0);
                        showPos[i] = new Vector3(-halfShowX, -halfShowY, 0);
                    }
                    else if (i == 2)
                    {
                        poses[i] = new Vector3(halfX, -halfY, 0);
                        showPos[i] = new Vector3(halfShowX, -halfShowY, 0);
                    }
                    else if (i == 3)
                    {
                        poses[i] = new Vector3(halfX, halfY, 0);
                        showPos[i] = new Vector3(halfShowX, halfShowY, 0);
                    }
                }
                for (int i = 0; i < 4; i++)
                {
                    if (i < 3)
                    {
                        Gizmos.DrawLine(transform.position + showPos[i], transform.position + showPos[i + 1]);
                    }
                    else
                    {
                        Gizmos.DrawLine(transform.position + showPos[i], transform.position + showPos[0]);
                    }
                }
            }
        }
#endif
    }
}